#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Service;
using System.Collections;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class RoleMasterSearchForm : Form
    {
        User m_current_user;
        UserService m_userService;
        UserControlUser m_parentForm;
        Keys m_currentKey;

        public RoleMasterSearchForm(User user, UserControlUser parent)
        {
            InitializeComponent();
            m_current_user = user;
            m_parentForm = parent;
            m_userService = (UserService)ServiceFactory.Instance().GetService(ServiceFactory.USERSERVICE);
            textBoxSearch.SelectAll();
            loadAllUser();
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            listViewRecords.Items.Clear();
            IList users = m_userService.SearchUserByUserCodeName(textBoxSearch.Text);
            foreach (User user in users)
            {
                ListViewItem item = listViewRecords.Items.Add(user.UserCode);
                item.SubItems.Add(user.UserName);
                item.Tag = user;
            }
            if (users.Count > 1)
            {
                listViewRecords.Focus();
                listViewRecords.SelectedIndex = 0;
            }
            else if (users.Count == 0) { textBoxSearch.SelectAll(); }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            User user = (User)listViewRecords.SelectedItem.Tag;
            m_parentForm.LoadUserFromSearch(user);
            this.Close();
        }

        private void loadAllUser()
        {
            listViewRecords.Items.Clear();
            IList users = m_userService.LoadAllUser();
            foreach (User user in users)
            {
                ListViewItem item = listViewRecords.Items.Add(user.UserCode);
                item.SubItems.Add(user.UserName);
                item.Tag = user;
            }
        }

        private void listViewRecords_DoubleClick(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            User user = (User)listViewRecords.SelectedItem.Tag;
            m_parentForm.LoadUserFromSearch(user);
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            m_currentKey = objArgs.KeyData;
        }

      
        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (m_currentKey == Keys.Enter)
            {
                buttonFind_Click(null, null);
            }
        }

        private void listViewRecords_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (objArgs.KeyCode == Keys.Enter)
            {
                listViewRecords_DoubleClick(objSender, null);
            }
        }

        private void textBoxSearch_GotFocus(object sender, EventArgs e)
        {
            textBoxSearch.SelectAll();
        }
    }
}